package com.cari.oa.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.cari.oa.domain.CustomerContactEntity;
import com.cari.oa.service.CustomerContactService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.DateUtils;
import com.cari.oa.util.Utils;

@Controller
@SessionAttributes({"username", "usercnname", "provinces"})
public class CustomerContactController {
	@Resource
	private CustomerContactService customerContactService;
	public static final String MODULE_NAME = "customercontacts";
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}

	/**
	 * 分页查找联系人信息
	 * @author -- liuyoujie
	 * @2013-6-20 下午3:10:15
	 * @param name			联系人名称
	 * @param company		联系人公司名称
	 * @param province		联系人省份
	 * @param curpage		当前页
	 * @param totalPage		总的页数
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("req/customercontacts/list.do")
	public String search(String name, String company, String province, 
			@ModelAttribute("provinces") Map<String, String> provinces,
			String starttime, String endtime,
			Integer curPage, ModelMap modelMap){
		if(curPage == null)	curPage = 1;
		List<CustomerContactEntity> contactList = customerContactService.getPage(name, company, province, 
				DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT),
				DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT),
				curPage, Contants.PAGE_SIZE);
		long totalRecords = customerContactService.getCount(name, company, province,
				DateUtils.str2Timestamp(starttime, DateUtils.NORMAL_FORMAT),
				DateUtils.str2Timestamp(endtime, DateUtils.NORMAL_FORMAT));
		long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
		
		modelMap.addAttribute("provinces", provinces);
		modelMap.addAttribute("contactList", contactList);
		modelMap.addAttribute("starttime", starttime);
		modelMap.addAttribute("endtime", endtime);
		modelMap.addAttribute("name", name);
		modelMap.addAttribute("company", company);
		modelMap.addAttribute("province", province);
		modelMap.addAttribute("curPage", curPage);
		modelMap.addAttribute("totalPage", totalPage);
		modelMap.addAttribute("totalRecords", totalRecords);
		
		return "req/customercontacts/list";
	}
	
	/**
	 * 新增客户联系人
	 * @author -- liuyoujie
	 * @2013-6-20 下午5:06:47
	 * @return
	 */
	@RequestMapping(value="req/customercontacts/insert.do")
	public String insert(ModelMap modelMap, String qs){
		modelMap.addAttribute("qs", qs);
		return "req/customercontacts/insert";
	}
	
	@RequestMapping(value="req/customercontacts/insert.do", method = RequestMethod.POST)
	@ResponseBody
	public String insert(@Valid CustomerContactEntity entity, @ModelAttribute("username") String username){
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		customerContactService.insert(entity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "新增客户联系人信息"+JSON.toJSONString(entity));
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="req/customercontacts/update.do")
	public String update(ModelMap modelMap, String qs, String id){
		CustomerContactEntity entity = customerContactService.getById(id);
		if(entity == null)	return Utils.errorJsonResp("未找到实体对象");
		
		modelMap.addAttribute("item", entity);
		modelMap.addAttribute("qs", qs);
		return "req/customercontacts/update";
	}
	
	@RequestMapping(value="req/customercontacts/update.do", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid CustomerContactEntity entity, @ModelAttribute("username") String username){
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		customerContactService.update(entity);
		SysOperLogController.addOperLog(username, MODULE_NAME, "修改客户联系人信息"+JSON.toJSONString(entity));
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value="req/customercontacts/delete.do")
	@ResponseBody
	public String delete(ModelMap modelMap, String qs, @ModelAttribute("username") String username, String id){
		CustomerContactEntity entity = customerContactService.getById(id);
		if(entity == null)	return Utils.errorJsonResp("未找到实体对象");
		customerContactService.delete(entity);
		modelMap.addAttribute("qs", qs);
		
		SysOperLogController.addOperLog(username, MODULE_NAME, "删除客户联系人信息"+JSON.toJSONString(entity));
		return Utils.normalJsonResp();
	}
}
